********************************************************************************
*** Chapter 3: ADL models of party brands (Model Specification of Stability and Change section)
***
*** Created: 12-18-17
*** Modified: 6-18-24
***
*** Note: I input (by hand) the estimates into three text files for use in creating the figures in R: DynSim.txt, Estimates.txt, and Total.txt
********************************************************************************

clear 
clear matrix
clear mata
set maxvar 30000
set seed 2517

*** Working directory
cd "C:\Users\williamslaro\Dropbox\partyBrands\elementsText"

use "data\analyticalSample.dta", clear

********************************************************************************
*** Data Management
********************************************************************************
* Generate a monthly time series variable
gen ts = ym(year, month)
format ts %tm

* Change the names of a couple variables
rename cmpPointLag cmpPoint_tm1
rename sophiaMean sophia

* Change the range of the manifestos variables to be 0 - 10
replace cmpPoint = (cmpPoint + 5)
replace cmpPoint_tm1 = (cmpPoint_tm1 + 5)

* Generate some lagged variables
sort cmp ts
bys cmp: gen sophia_tm1 = sophia[_n-1]


********************************************************************************
*** Time Series Specifications
********************************************************************************

* General: ADL (1;1)
preserve
	estsimp reg sophia sophia_tm1 cmpPoint cmpPoint_tm1

	gen alpha = b1
	gen ste_t = b2
	gen ste_tm1 = b3
	gen lte = (b2 + b3) / (1 - alpha)

	foreach v in alpha ste_t ste_tm1 lte {
		qui sum `v'
		local se = r(sd)
				
		_pctile `v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos QI = `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"	
	}
	
	gen effect_0 = b2
	gen effect_1 = b3 + (effect_0 * alpha)
	gen effect_2 = (effect_1 * alpha)
	gen effect_3 = (effect_2 * alpha)
	gen effect_4 = (effect_3 * alpha)
	gen effect_5 = (effect_4 * alpha)

	foreach v of numlist 0(1)5 {
		qui sum effect_`v'
		local se = r(sd)
				
		_pctile effect_`v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos Effect at time `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"			
	}
	
	gen total3 = (effect_0 + effect_1 + effect_2 + effect_3)
	qui sum total3
	local se = r(sd)
				
	_pctile total3, p(2.5 5 50 95 97.5)
			
	di _newline(2) "Manifestos Total Effect from t to t+3"
	di "Median = `r(r3)'; Standard Error = `se'"
	di "90% confidence interval = [`r(r2)', `r(r4)']"
	di "95% confidence interval = [`r(r1)', `r(r5)']"			
restore


* Partial Adjustment: ADL(1;0)
preserve
	estsimp reg sophia sophia_tm1 cmpPoint 

	gen alpha = b1
	gen ste_t = b2
	gen lte = (b2) / (1 - alpha)

	foreach v in alpha ste_t lte {
		qui sum `v'
		local se = r(sd)
				
		_pctile `v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos QI = `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"	
	}
	
	gen effect_0 = b2
	gen effect_1 = (effect_0 * alpha)
	gen effect_2 = (effect_1 * alpha)
	gen effect_3 = (effect_2 * alpha)
	gen effect_4 = (effect_3 * alpha)
	gen effect_5 = (effect_4 * alpha)

	foreach v of numlist 0(1)5 {
		qui sum effect_`v'
		local se = r(sd)
				
		_pctile effect_`v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos Effect at time `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"			
	}	
	
	gen total3 = (effect_0 + effect_1 + effect_2 + effect_3)
	qui sum total3
	local se = r(sd)
				
	_pctile total3, p(2.5 5 50 95 97.5)
			
	di _newline(2) "Manifestos Total Effect from t to t+3"
	di "Median = `r(r3)'; Standard Error = `se'"
	di "90% confidence interval = [`r(r2)', `r(r4)']"
	di "95% confidence interval = [`r(r1)', `r(r5)']"				
restore


* Dead Start: ADL(1;1)
preserve
	estsimp reg sophia sophia_tm1 cmpPoint_tm1 
	
	gen alpha = b1
	gen ste_tm1 = b2
	gen lte = (b2) / (1 - alpha)

	foreach v in alpha ste_tm1 lte {
		qui sum `v'
		local se = r(sd)
				
		_pctile `v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos QI = `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"	
	}
	
	gen effect_0 = 0
	gen effect_1 = b2 + (effect_0 * alpha)
	gen effect_2 = (effect_1 * alpha)
	gen effect_3 = (effect_2 * alpha)
	gen effect_4 = (effect_3 * alpha)
	gen effect_5 = (effect_4 * alpha)

	foreach v of numlist 0(1)5 {
		qui sum effect_`v'
		local se = r(sd)
				
		_pctile effect_`v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos Effect at time `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"			
	}	
	
	gen total3 = (effect_0 + effect_1 + effect_2 + effect_3)
	qui sum total3
	local se = r(sd)
				
	_pctile total3, p(2.5 5 50 95 97.5)
			
	di _newline(2) "Manifestos Total Effect from t to t+3"
	di "Median = `r(r3)'; Standard Error = `se'"
	di "90% confidence interval = [`r(r2)', `r(r4)']"
	di "95% confidence interval = [`r(r1)', `r(r5)']"				
restore

* Finite Distributed Lag: ADL(0;1)
preserve
	estsimp reg sophia cmpPoint cmpPoint_tm1 

	gen ste_t = b1
	gen ste_tm1 = b2

	foreach v in ste_t ste_tm1 {
		qui sum `v'
		local se = r(sd)
				
		_pctile `v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos QI = `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"	
	}
	
	gen effect_0 = b1
	gen effect_1 = b2
	gen effect_2 = 0
	gen effect_3 = 0
	gen effect_4 = 0
	gen effect_5 = 0

	foreach v of numlist 0(1)5 {
		qui sum effect_`v'
		local se = r(sd)
				
		_pctile effect_`v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos Effect at time `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"			
	}	
	
	gen total3 = (effect_0 + effect_1 + effect_2 + effect_3)
	qui sum total3
	local se = r(sd)
				
	_pctile total3, p(2.5 5 50 95 97.5)
			
	di _newline(2) "Manifestos Total Effect from t to t+3"
	di "Median = `r(r3)'; Standard Error = `se'"
	di "90% confidence interval = [`r(r2)', `r(r4)']"
	di "95% confidence interval = [`r(r1)', `r(r5)']"				
restore

* Static: ADL(0;0)
preserve
	estsimp reg sophia cmpPoint 
	
	gen ste_t = b1

	foreach v in ste_t {
		qui sum `v'
		local se = r(sd)
				
		_pctile `v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos QI = `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"	
	}
	
	gen effect_0 = b1
	gen effect_1 = 0
	gen effect_2 = 0
	gen effect_3 = 0
	gen effect_4 = 0
	gen effect_5 = 0

	foreach v of numlist 0(1)5 {
		qui sum effect_`v'
		local se = r(sd)
				
		_pctile effect_`v', p(2.5 5 50 95 97.5)
			
		di _newline(2) "Manifestos Effect at time `v'"
		di "Median = `r(r3)'; Standard Error = `se'"
		di "90% confidence interval = [`r(r2)', `r(r4)']"
		di "95% confidence interval = [`r(r1)', `r(r5)']"			
	}	
	
	gen total3 = (effect_0 + effect_1 + effect_2 + effect_3)
	qui sum total3
	local se = r(sd)
				
	_pctile total3, p(2.5 5 50 95 97.5)
			
	di _newline(2) "Manifestos Total Effect from t to t+3"
	di "Median = `r(r3)'; Standard Error = `se'"
	di "90% confidence interval = [`r(r2)', `r(r4)']"
	di "95% confidence interval = [`r(r1)', `r(r5)']"				
restore
